package com.fone.player;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import cn.sharesdk.framework.ShareSDK;

import com.fone.player.billing.impl.FonePayManager;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.client.Reporter;
import com.fone.player.client.Request;
import com.fone.player.entity.LabelListlInfo;
import com.fone.player.service.FoneServiceManager;
import com.fone.player.sns.UserInfoManager;
import com.fone.player.sns.util.SNSUtil;
import com.fone.player.storage.SharedPreferenceModule;
import com.fone.player.storage.StorageModule;
import com.fone.player.storage.UpgradeDataBaseAdapter;
import com.fone.player.util.ActivityQueue;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.LoginUtil;
import com.sohuvideo.sdk.SohuVideoPlayer;

public class ApplicationManage extends Application {

	private static ApplicationManage appInstances;
	public LabelListlInfo labelList = new LabelListlInfo();

	@Override
	public void onCreate() {
		super.onCreate();
		L.v("ApplicationManage", "onCreate", "start");

		// 初始化SharedPreference 模块
		SharedPreferenceModule.init(this);

		appInstances = this;
		// 初始化SNS 应用启用时调用
		SNSUtil.initSNS(getApplicationContext());
		// 初始化用户管理
		UserInfoManager.initUserInfoManager(getApplicationContext());

		// 初始化存储模块
		StorageModule.init(this);

		// 初始化图片缓存模块
		ImageDownloadModule.init(this);

		// 升级用户(只运行一次)
		UpgradeDataBaseAdapter.getInstance().upgradeUser();

		// 初始化网络请求
		Request.initializeInstance(getApplicationContext());

		// 开启通知服务
		FoneServiceManager.getFoneServiceManager(getApplicationContext())
				.startNotificationService();

		// 自动登录
		if (UserInfoManager.isLogin()) {
			LoginUtil.autoLogin();
		}

		// 初始化计费manager 检测默认情况下用户是否计费 是否需要过有效期
		FonePayManager.initFonePayManagerInstance(getApplicationContext());

		// 启动数据上报服务
		// Reporter.start();
		try {

			PackageManager pm = getPackageManager();

			PackageInfo pinfo;

			pinfo = pm.getPackageInfo(getPackageName(),
					PackageManager.GET_CONFIGURATIONS);
			
			Log.i("Report", "current version" + pinfo.versionCode);
			
			int ver = SharedPreferenceModule.getInstance().getInt("version",
					pinfo.versionCode);
			
			Log.i("Report", "get version" + ver);

			if (ver < pinfo.versionCode) {
				if (SharedPreferenceModule.getInstance().getBoolean("upgrade_self_check")) {
					Reporter.logEvent(Reporter.EventId.UPGRADE_SELF_CHECK);
				} else {
					Reporter.logEvent(Reporter.EventId.UPGRADE_DOWNLOAD);
				}
			}
			
			SharedPreferenceModule.getInstance().setInt("version", pinfo.versionCode);

		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		FoneUtil.copyDefaultShareimage();

//		//搜狐sdk初始化
		SohuVideoPlayer.init(getApplicationContext());
		
	}

	public static ApplicationManage getAplicationManageInstance() {
		/*
		 * Application实例不能 new
		 * 
		 * if (null == appInstances) { appInstances = new ApplicationManage(); }
		 */
		return appInstances;
	}

	/**
	 * exit app
	 * 
	 * @return void
	 * @throws
	 */
	public void exitSystem() {
		ActivityQueue.finishAllActivity();
		AppExit(this);
	}

	/**
	 * 获取全局的context
	 * 
	 * @return
	 */
	public static Context getGlobalContext() {
		return appInstances.getApplicationContext();
	}

	/**
	 * 退出应用程序
	 */
	private void AppExit(Context context) {
		// 停止缓存下载服务
		StorageModule.getInstance().stopCacheService();
		ShareSDK.stopSDK(context);
		System.exit(0);
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}
